//
//  KRLTimelineViewController.m
//  KaraLive
//
//  Created by Zhijia Zhong on 2018/9/14.
//  Copyright © 2018年 naxigoren. All rights reserved.
//

#import "KRLTimelineViewController.h"
#import "KRLBannerView.h"
#import "KRLTimelineModel.h"
#import "KRLTimelineCell.h"
#import "KRLTimelineKTVCell.h"
#import "KRLTimelineRecordCell.h"
#import "KRLTimelineVLogCell.h"

@interface KRLTimelineViewController ()

@property (nonatomic, strong) NSMutableArray *timelines;

@property (nonatomic) NSInteger currentPage;

@end

@implementation KRLTimelineViewController

- (NSMutableArray *)timelines {
    if (!_timelines) {
        _timelines = [NSMutableArray array];
    }
    return _timelines;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"Timeline";
    self.navigationController.navigationBar.titleTextAttributes = @{
                                                                    NSForegroundColorAttributeName: [UIColor colorWithRGB:0xff9451],
                                                                    NSFontAttributeName: [UIFont systemFontOfSize:22 weight:UIFontWeightBold]
                                                                    };

    self.tableView.rowHeight = 300;
    for (NSInteger type = 0; type < 4; type++) {
        Class class = [self cellClassForType:type];
        NSString *identifier = [self cellReuseIdentifier:type];
        [self.tableView registerClass:class forCellReuseIdentifier:identifier];
    }

    CGRect rect = self.tableView.frame;
    rect.size.height = 180;
    UIImage *image1 = [UIImage imageNamed:@"kobe1"];
    UIImage *image2 = [UIImage imageNamed:@"kobe2"];
    KRLBannerView *bannerView = [[KRLBannerView alloc] initViewWithFrame:rect timeInterval:1 imagesArray:@[image1, image2] clickCallBack:^(NSInteger index) {
        
    }];
    self.tableView.tableHeaderView = bannerView;

    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [@{
                             @"api_code": @"Home/getMainForumList",
                             @"user_token": KRLUserModel.sharedUser.userToken,
                             @"page": @1,
                             @"pageSize": @10,
                             } mutableCopy];
    [session POST:@"http://api.kara.live/apiv2/Home/getMainForumList" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *data in responseObject[@"results"]) {
            KRLTimelineModel *model = [KRLTimelineModel yy_modelWithDictionary:data];
            [self.timelines addObject:model];
            NSLog(@"%@", model);
        }
        [self.tableView reloadData];
        self.currentPage = 1;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"login failure");
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.timelines removeAllObjects];
        params[@"page"] = @1;
        [session POST:@"http://api.kara.live/apiv2/Home/getMainForumList" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            for (NSDictionary *data in responseObject[@"results"]) {
                KRLTimelineModel *model = [KRLTimelineModel yy_modelWithDictionary:data];
                [self.timelines addObject:model];
                NSLog(@"%@", model);
            }
            [self.tableView reloadData];
            self.currentPage = 1;
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"login failure");
        }];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        params[@"page"] = @(self.currentPage + 1);
        [session POST:@"http://api.kara.live/apiv2/Home/getMainForumList" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            for (NSDictionary *data in responseObject[@"results"]) {
                KRLTimelineModel *model = [KRLTimelineModel yy_modelWithDictionary:data];
                [self.timelines addObject:model];
                NSLog(@"%@", model);
            }
            [self.tableView reloadData];
            self.currentPage++;
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"login failure");
        }];
    }];
}

- (Class)cellClassForType:(KRLTimelineType)type {
    switch (type) {
        case KRLTimelineTypeKTV:
            return [KRLTimelineKTVCell class];
            break;
        case KRLTimelineTypeRecord:
            return [KRLTimelineRecordCell class];
            break;
        case KRLTimelineTypeVLog:
            return [KRLTimelineVLogCell class];
            break;
        default:
            return [KRLTimelineCell class];
            break;
    }
}

- (NSString *)cellReuseIdentifier:(KRLTimelineType)type {
    return NSStringFromClass([self cellClassForType:type]);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.timelines.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KRLTimelineModel *model = self.timelines[indexPath.row];
    NSString *reuseIdentifier = [self cellReuseIdentifier:model.type];
    KRLTimelineCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];
    [cell updateCellWithModel:model];
    
    return cell;
}

@end
